﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using OxyPlot;

namespace DataInterpolator.Model
{
   public class Well_OutputData
   {
      public Well_OutputData()
      {
         Data = new List<DataPoint>();
      }

      [XmlIgnore()]
      public double XOffset
      {
         get; private set;
      }

      [XmlIgnore()]
      public double YOffset
      {
         get; private set;
      }

      public List<DataPoint> Data
      {
         get; private set;
      }

      private double FindFirstValue(IList<double> values)
      {
         return values.First((d) =>
         {
            return !double.IsNaN(d);
         });
      }

      public void Calc(Well well, string zeroRef, double windowMin, double windowMax)
      {
         Data.Clear();

         double refDepth = 0.0;
         double refTime = 0.0;

         if (well.ExtrapolatedData.RefValues.ContainsKey(zeroRef))
         {
            refDepth = well.ExtrapolatedData.RefValues[zeroRef].Depth;
            refTime = well.ExtrapolatedData.RefValues[zeroRef].Time;
         }
         else
         {
            throw new Exception("Zero reference: " + zeroRef + ", not found.");
         }

         for (int i = 0; i < well.ExtrapolatedData.Data.Count; i++)
         {
            var point = new DataPoint(well.ExtrapolatedData.Data[i].X, well.ExtrapolatedData.Data[i].Y);

            point.X -= refTime;
            point.Y -= refDepth;

            //if (point.Y < 0.0)
            //   continue;

            if (point.Y < windowMin || point.Y > windowMax)
               continue;

            if (double.IsNaN(point.Y))
               continue;

            Data.Add(point);
         }

         //Save the offset
         XOffset = refTime;
         YOffset = refDepth;
      }
   }
}
